<template>
  <div class="score-case">
    <div class="table">
      <table>
        <thead>
          <tr>
            <th>编号</th>
            <th>科目</th>
            <th>成绩</th>
            <th>考试时间</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <!-- 步骤一，对list中的数组 进行v-for遍历 添加到td中-->
            
            <tr v-for="(item,index) in list " :key="item.id">
            <td>{{index + 1}}</td> 
            <td>{{item.subject}}</td>
            <td :class="item.score < 60 ? 'red' : '' ">{{item.score}}</td>   <!-- 利用三元运算符筛选将小于60分标记为红色 -->
            <td>{{rightDate(item.date)}}</td>
            <td><a href="#" @click="delFn(item.id)">删除</a></td>
          </tr>
          <!-- <tr >
            <td>1</td>
            <td>语文</td>
            <td class="red">56</td>
            <td>Tue Jun 07 2022 10:00:00 GMT+0800 (中国标准时间)</td>
            <td><a href="#">删除</a></td>
          </tr> -->
          <!-- <tr >
            <td>2</td>
            <td>数学</td>
            <td>100</td>
            <td>Tue Jun 07 2022 10:00:00 GMT+0800 (中国标准时间)</td>
            <td><a href="#">删除</a></td>
          </tr> -->
        </tbody>
        <!-- <tbody >
          <tr>
            <td colspan="5">
              <span class="none">暂无数据</span>
            </td>
          </tr>
        </tbody> -->
        <tfoot>
          <tr>
            <td colspan="5">
              <span>总分：{{sumFn}}</span>
              <span style="margin-left:50px">平均分：{{aveFn}}</span>
            </td>
          </tr>
        </tfoot>
      </table>
    </div>
    <div class="form">
      <div class="form-item">
        <div class="label">科目：</div>
        <div class="input">
          <input type="text" placeholder="请输入科目" v-model.trim="subject"/>
        </div>
      </div>
      <div class="form-item">
        <div class="label">分数：</div>
        <div class="input">
          <input type="text" placeholder="请输入分数" v-model.number="score"/>
        </div>
      </div>
      <div class="form-item">
        <div class="label"></div>
        <div class="input">
          <button class="submit"  @click="addFn">添加</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
export default {
  name: 'ScoreCase',
  data () {
    return {
      // list: [
      //   {id: 15, subject: '语文', score: 89, date: new Date('2022/06/07 10:00:00')},
      //   {id: 27, subject: '数学', score: 100, date: new Date('2022/06/07 15:00:00')},
      //   {id: 32, subject: '英语', score: 56, date: new Date('2022/06/08 10:00:00')},
      //   {id: 41, subject: '物理', score: 76, date: new Date('2022/06/08 10:00:00')}
      // ],
      list: JSON.parse(localStorage.getItem('score-case')) ,
      subject: '',
      score: '',
    }
  },
    //  步骤三 计算总和和平均数 需用计算属性并写入插值表达式
    computed: {
      sumFn() {
         return  this.list.reduce((sum,item) => sum + item.score,0) //computed中必须要有return
     },
      aveFn() {
        return  (this.sumFn /  this.list.length).toFixed(2) //.toFixed(2) 表示保留两位小数
      }
    },

  methods: {
    // 步骤二 删除功能
     delFn(id) {
      //  通过声明index的方式，找到item.id 从而删除对应元素的索引值
        const index = this.list.findIndex(item => item.id === item.id) //两个item指的不是同一种item
        this.list.splice(index,1)
     },
     //步骤四 添加功能 必须用v-model 进行数据的双向绑定 记住!记住!记住!  v-model v-model v-model
     addFn() {
      //  此处需要加一个判断条件避免用户输入100分以上或者0分以下
      if (this.score >= 0 && this.score <= 100 && this.score) {
         this.list.push({
          id: this.list.length ? this.list.at(-1).id + 1 : 1, //此处必须加一个判断语句 
          subject: this.subject,
          score: this.score ,
          date: new Date()
       })  //记住此处是push一个对象而非数组 
       alert('真棒！')
      } else {
        alert('输入格式不正确，请重新输入')
      }
      // 清空表单
      this.subject = '',
      this.score = ''
     },
     
    // 步骤五 将时间改为正确格式  先yarn add moment  下载一个moment插件 并引入moment模块
   rightDate(dt) {
      return  moment(dt).format("YYYY-MM-DD HH:mm:ss") //此处可去http://momentjs.cn/ 进行CV
    },

    // 步骤六 用属性监听来引入 JSON.stringify() 
   watch: {
    list: {
      deep:true, //代表深度监听
      handler() {
         localStorage.setItem('score-case',JSON.stringify(this.list))
      }
    }
  }
  },
};
</script>

<style lang="less">
.score-case {
  width: 1000px;
  margin: 50px auto;
  display: flex;
  .table {
    flex: 4;
    table {
      width: 100%;
      border-spacing: 0;
      border-top: 1px solid #ccc;
      border-left: 1px solid #ccc;
      th {
        background: #f5f5f5;
      }
      tr:hover td {
        background: #f5f5f5;
      }
      td,
      th {
        border-bottom: 1px solid #ccc;
        border-right: 1px solid #ccc;
        text-align: center;
        padding: 10px;
        &.red {
          color: red;
        }
      }
    }
    .none {
      height: 100px;
      line-height: 100px;
      color: #999;
    }
  }
  .form {
    flex: 1;
    padding: 20px;
    .form-item {
      display: flex;
      margin-bottom: 20px;
      align-items: center;
    }
    .form-item .label {
      width: 60px;
      text-align: right;
      font-size: 14px;
    }
    .form-item .input {
      flex: 1;
    }
    .form-item input,
    .form-item select {
      appearance: none;
      outline: none;
      border: 1px solid #ccc;
      width: 200px;
      height: 40px;
      box-sizing: border-box;
      padding: 10px;
      color: #666;
    }
    .form-item input::placeholder {
      color: #666;
    }
    .form-item .cancel,
    .form-item .submit {
      appearance: none;
      outline: none;
      border: 1px solid #ccc;
      border-radius: 4px;
      padding: 4px 10px;
      margin-right: 10px;
      font-size: 12px;
      background: #ccc;
    }
    .form-item .submit {
      border-color: #069;
      background: #069;
      color: #fff;
    }
  }
}
</style>